package com.twb.code.gen.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twb.code.gen.entity.twbCodeGen.TwbCodeGen;
import com.twb.code.gen.entity.twbCodeGen.dto.ExportExcelTwbCodeGenDto;
import com.twb.code.gen.entity.twbCodeGen.dto.SearchDataDicKeyDto;
import com.twb.code.gen.entity.twbCodeGen.dto.SearchTableColumnsDto;
import com.twb.code.gen.entity.twbCodeGen.dto.SearchTableDto;
import com.twb.code.gen.entity.twbCodeGen.dto.SearchTwbCodeGenDto;
import com.twb.code.gen.entity.twbCodeGen.vo.SaveTwbCodeGenVo;
import com.twb.code.gen.entity.twbCodeGen.vo.SearchTwbCodeGenVo;
import com.twb.code.gen.entity.twbCodeGen.vo.UpdateTwbCodeGenByIdVo;
import com.twb.code.gen.mapper.TwbCodeGenMapper;
import com.twb.code.gen.service.ITwbCodeGenService;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;

/**
 * @ClassName TwbCodeGenService
 * @Description 代码生成配置service
 * 
 * @author DSY
 * @date 2021-12-31 15:30:38
 */
@Service("twbCodeGenService")
public class TwbCodeGenService extends BaseService<TwbCodeGenMapper, TwbCodeGen> implements ITwbCodeGenService {

	@Autowired
	TwbCodeGenMapper twbCodeGenMapper;

	@Override
	public TwbPageInfo<SearchTwbCodeGenDto> searchTwbCodeGen(SearchTwbCodeGenVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchTwbCodeGenDto> list = twbCodeGenMapper.searchTwbCodeGen(vo);
		return TwbPageUtil.of(list);
	}

	@Override
	public List<SearchTwbCodeGenDto> searchTwbCodeGenList(SearchTwbCodeGenVo vo) {
		List<SearchTwbCodeGenDto> list = twbCodeGenMapper.searchTwbCodeGen(vo);
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveTwbCodeGen(SaveTwbCodeGenVo vo) {
		TwbCodeGen checkVo = new TwbCodeGen();
		checkVo.setName(vo.getName()); // 此处需要修改为需要校验重复的字段
		checkVo.setIsdel(0);
		long count = this.count(checkVo);
		if (count > 0) {
			throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
		}

		TwbCodeGen record = new TwbCodeGen();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(this.getUUID());
		this.setCreateInfo(record);
		this.setUpdateInfo(record);
		return this.save(record);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateTwbCodeGenById(UpdateTwbCodeGenByIdVo vo) {
		TwbCodeGen record = twbCodeGenMapper.selectById(vo.getId());
		if (null == record) {
			throw new ServiceException("数据不存在");
		}

//		if (!vo.getName().equalsIgnoreCase(record.getName())) {
//			TwbCodeGen checkVo = new TwbCodeGen();
//			checkVo.setName(vo.getName()); // 此处需要修改为需要校验重复的字段
//			checkVo.setIsdel(0);
//			long count = this.count(checkVo);
//			if (count > 0) {
//				throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
//			}
//		}

		BeanUtils.copyNotNullProperties(vo, record);
		this.setUpdateInfo(record);
		return this.updateById(record);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteTwbCodeGenById(String id) {
		return this.removeById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteTwbCodeGenByIds(String ids) {
		return this.removeByIds(Arrays.asList(ids.split(",")));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int logicBatchDeleteTwbCodeGenByIds(String ids) {
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, ids.split(","));
		return twbCodeGenMapper.logicBatchDeleteByIds(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void exportExcel(SearchTwbCodeGenVo vo, HttpServletResponse response) throws Exception {
		List<ExportExcelTwbCodeGenDto> list = twbCodeGenMapper.searchExportTwbCodeGen(vo);
		ExcelUtils.exportExcel("代码生成配置", list, ExportExcelTwbCodeGenDto.class, response);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int importExcel(Map<String, String> map) {
		System.out.println(map.toString());
		return 0;
	}

	@Override
	public List<SearchTableDto> searchTables(String queryLikeStr) {

		List<SearchTableDto> tableList = twbCodeGenMapper.searchMysqlTables(queryLikeStr);

		return tableList;
	}

	@Override
	public List<SearchTableColumnsDto> searchColumns(String tableName) {
		List<SearchTableColumnsDto> columnsList = twbCodeGenMapper.searchMysqlColumns(tableName);
		return columnsList;
	}

	@Override
	public List<SearchDataDicKeyDto> searchDataDicKey() {
		List<SearchDataDicKeyDto> searchDataDicKey = twbCodeGenMapper.searchDataDicKey();
		return searchDataDicKey;
	}
}
